package com.yuandian.business.monitor.service.impl;

import com.yuandian.api.business_monitor.po.ShardResult;
import com.yuandian.business.monitor.service.ITableConfigHelpService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 表分区创建配置表service实现类(迁自2.5)
 *
 * @author LinShiHe
 * @date 2020-06-04
 *
 */
@Service
public class TableConfigServiceHelpImpl implements ITableConfigHelpService {

    /**
     * 查询Bpm天统计表
     *
     * @param tableNameList
     * @param tableNameDto
     */
    @Override
    public void getBpmTableMapDay(List<Map<String, String>> tableNameList, ShardResult tableNameDto) {

        LocalDateTime startTime = tableNameDto.getStartTime();
        LocalDateTime endTime = tableNameDto.getEndTime();

        // 获取结束时间 的0分0秒
        int sumEnd = endTime.getHour() + endTime.getMinute() + endTime.getSecond();

        // 由于按天统计表目前都是按月存放或者原始表，同时天表的时间里面分钟都置0，那么进来的开始时间分钟需要置0，结束时间需要加1小时，分钟置0
        LocalDateTime startDateTime = startTime.withHour(0).withSecond(0);

        // 获取结束时间 的0分0秒
        LocalDateTime endDateTime = endTime.withHour(0).withSecond(0);
        if (sumEnd != 0) {
            endDateTime = endDateTime.plusDays(1);
        }

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        Map<String, String> map = new HashMap<>();
        map.put("tableName", tableNameDto.getTable());
        map.put("startTime", formatter.format(startDateTime));
        map.put("endTime", formatter.format(endDateTime));
        tableNameList.add(map);
    }

}
